//
//  SZLSellerGoodsManageViewController.m
//  Shengzhuli
//
//  Created by subook on 16/8/11.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLSellerGoodsManageViewController.h"
#import "GoodsManageTableViewCell.h"
#import "SZLSellerAddGoodsViewController.h"
#import "SZLAccountHelper.h"
#import <MJRefresh.h>
#import "BusinessNetwork.h"
#import "MBProgressHUD+MJ.h"

@interface SZLSellerGoodsManageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger pageNo;

@end
@implementation SZLSellerGoodsManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商品管理";
    self.pageNo = 1;
    [self configNav];
    [self createTableView];
//    [self requestGoodsList];
}
- (void)requestGoodsList
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"pageNo"]  = @(self.pageNo);
    [BusinessNetwork getShopGoodsListWithParame:params response:^(BOOL success, NSString *msg, NSArray *lists) {
        if (success) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            self.tableView.mj_footer.hidden = (lists.count == 0)?YES:NO;
            if (self.pageNo == 1) {
                if (lists.count == 0) {
                    [self showNoDataView:self.view.bounds title:@"暂无商品"];
                    return ;
                }else if(lists.count<20){
                    self.tableView.mj_footer.hidden = YES;
                }
                [self hideNoDataView];
                [self.dataArray removeAllObjects];
            }else{
                if (lists.count == 0) {
                    self.pageNo--;
                    return;
                }
            }
            [self.dataArray addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)configNav
{
    UIButton *rightItemButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    rightItemButton.imageEdgeInsets = UIEdgeInsetsMake(8, 16, 8, 0);
    [rightItemButton setImage:[UIImage imageNamed:@"btn_add_normal"] forState:(UIControlStateNormal)];
    [rightItemButton setImage:[UIImage imageNamed:@"btn_add_pressed"] forState:(UIControlStateHighlighted)];
    [rightItemButton sizeToFit];
    [rightItemButton addTarget:self action:@selector(addGoodsClick:) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightItemButton];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)addGoodsClick:(UIButton *)button
{
    SZLSellerAddGoodsViewController *addVc = [[SZLSellerAddGoodsViewController alloc] init];
    addVc.type = SellerGoodsTypeAdd;
    __WEAK
    addVc.successBlock = ^(){
        [weakSelf requestGoodsList];
    };
    [self.navigationController pushViewController:addVc animated:YES];
}
- (void)createTableView
{
    UITableView * tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH-64))];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodsManageTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([GoodsManageTableViewCell class])];
    tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:tableView];
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNo = 1;
        [self requestGoodsList];
    }];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageNo++;
        [self requestGoodsList];
    }];
    self.tableView = tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return _dataArray.count;
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsManageTableViewCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GoodsManageTableViewCell class])];
//    goodsCell.model = self.dataArray[indexPath.row];
    return goodsCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    SZLSellerAddGoodsViewController *addVc = [[SZLSellerAddGoodsViewController alloc] init];
//    GoodsManageModel *model = self.dataArray[indexPath.row];
//    addVc.productNumber = model.productNo;
//    addVc.type = SellerGoodsTypeShow;
//    __WEAK
//    addVc.successBlock = ^(){
//        [weakSelf requestGoodsList];
//    };
//    [self.navigationController pushViewController:addVc animated:YES];
    
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
